<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Good
{
    public function cate()
    {
        $id = Request::get('id',0);

        $list = Db::name('category')
            ->where('parentid','=',$id)
            ->field(true)
            ->order('sort')
            ->select()
            ->toArray();

        if (Cache::has('category_list')){
            $all = Cache::get('category_list');
        }else{
            $all = Db::name('category')->field(true)->select()->toArray();
            Cache::set('category_list',$all,10);
        }

        foreach ($list as &$v){
            foreach ($all as $vv){
                if($v['id'] == $vv['parentid'])  $v['next'] = 1;
            }
        }



        View::assign('list',$list);

        return view();
    }

    public function cateEdit()
    {
        return view();
    }

    public function goodList()
    {
        $keywords = Request::get('keywords');
        View::assign('keywords',$keywords);
        $post = Request::get();
        $where = [];
        if (!empty($keywords))                                          $where[] = ['g.name','like','%'.$keywords."%"];
        if (isset($post['is_down']) && count($post['is_down']) == 1)    $where[] = ['is_down','=',$post['is_down'][0]];
        if(isset($post['is_show']) && $post['is_show'] <> '')           $where[] = ['is_show','=',$post['is_show']];
        if(isset($post['is_hot']) && $post['is_hot'] <> '')             $where[] = ['is_hot','=',$post['is_hot']];
        if(isset($post['cid']) && $post['cid'] <> '')             $where[] = ['cid','=',$post['cid']];
        if(isset($post['maxtime']) && $post['maxtime'] <> '')             $where[] = ['g.addtime','<= time',$post['maxtime']];
        if(isset($post['mintime']) && $post['mintime'] <> '')             $where[] = ['g.addtime','>=    time',$post['mintime']];
        if(isset($post['min']) && $post['min'] <> '')             $where[] = ['g.price','>=',$post['min']];
        if(isset($post['max']) && $post['max'] <> '')             $where[] = ['g.price','<=',$post['max']];

        View::assign($post);
        unset($post['order']);
        View::assign('post',$post);
        $order = '';
        if (Request::has('order')){
            $order = Request::get('order');
            if ($order == 'g.addtime desc') View::assign('order','g.addtime');
            if ($order == 'g.addtime')      View::assign('order','g.id desc');
            if ($order == 'g.id desc')      View::assign('order','g.id');
            if ($order == 'g.id')           View::assign('order','');
        }

        $arr=Db::table("ecm_category")->select();



        $list = Db::name('goods')
            ->alias('g')
            ->join('category c','g.cid = c.id')
            ->where($where)
            ->field('g.*')
            ->field('c.name cname')
            ->order($order)
            ->paginate([
                'list_rows' => 4,
                'query'     => $post
            ]);

        View::assign('list',$list);
        View::assign('arr',$arr);
        return view();
    }
    public function goodAdd()
    {

        return view();
    }
}
